package com.zhihuichongcheng.application.ui.approval.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;

import com.zhihuichongcheng.application.R;
import com.zhihuichongcheng.application.base.BaseActivity;
import com.zhihuichongcheng.application.ui.approval.fragment.AlreadyApprovalFragment;
import com.zhihuichongcheng.application.ui.approval.fragment.ApprovalSaveListFragment;
import com.zhihuichongcheng.application.ui.approval.fragment.MyApprovalFragment;
import com.zhihuichongcheng.application.ui.approval.fragment.SendTomeApprovalFragment;
import com.zhihuichongcheng.application.ui.approval.fragment.WaittingApprovalFragment;
import com.zhihuichongcheng.application.ui.main.activity.MainActivity;

/**
 * Created by 3water on 2016/11/13.
 */

public class ApprovalListActivity extends BaseActivity {
    private int mType;
    private WaittingApprovalFragment mWaittingApprovalFragment;
    private AlreadyApprovalFragment mAlreadyApprovalFragment;
    private MyApprovalFragment mMyApprovalFragment;
    private SendTomeApprovalFragment mSendTomeApprovalFragment;
    private ApprovalSaveListFragment mSaveApprovalFragment;
    private boolean mIsFromPush;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_approval_list);
        initView();
        initData(getIntent().getExtras(), false);
    }

    private void initView() {

    }

    private void initData(Bundle bundle, boolean replace) {
        if (bundle != null) {
            if (bundle.containsKey("title")) {
                String title = bundle.getString("title");
                setTitle(title);
            }
            if (bundle.containsKey("type")) {
                mType = bundle.getInt("type", -1);
            }
            mIsFromPush = bundle.getBoolean("isFromPush", false);
        }

        switch (mType) {
            case 0:
                mWaittingApprovalFragment = new WaittingApprovalFragment();
                getSupportFragmentManager().beginTransaction().add(R.id.container, mWaittingApprovalFragment).commit();
                break;
            case 1:
                mAlreadyApprovalFragment = new AlreadyApprovalFragment();
                if (replace)
                    getSupportFragmentManager().beginTransaction().replace(R.id.container, mAlreadyApprovalFragment).commit();
                else
                    getSupportFragmentManager().beginTransaction().add(R.id.container, mAlreadyApprovalFragment).commit();
                break;
            case 2:
                mMyApprovalFragment = new MyApprovalFragment();
                getSupportFragmentManager().beginTransaction().add(R.id.container, mMyApprovalFragment).commit();
                break;
            case 3:
                mSendTomeApprovalFragment = new SendTomeApprovalFragment();
                getSupportFragmentManager().beginTransaction().add(R.id.container, mSendTomeApprovalFragment).commit();
                break;
            case 4:
                mSaveApprovalFragment = new ApprovalSaveListFragment();
                getSupportFragmentManager().beginTransaction().add(R.id.container, mSaveApprovalFragment).commit();
                break;
            default:
                break;
        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == WaittingApprovalFragment.REQUEST_WAITING_APPROVAL_DETAIL && resultCode == RESULT_OK) {
            Bundle bundle = new Bundle();
            bundle.putInt("type", 1);
            bundle.putString("title", "我已审批");
            initData(bundle, true);
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (mIsFromPush) {
            Bundle bundle = new Bundle();
            bundle.putInt("index", 1);
            startActivity(MainActivity.class, bundle);
        }
        finish();
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mIsFromPush) {
            onBaClick(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mIsFromPush) {
                        Bundle bundle = new Bundle();
                        bundle.putInt("index", 1);
                        startActivity(MainActivity.class, bundle);
                    }
                    finish();
                }
            });
        }
    }
}
